﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAL;
using BUS;

namespace WEBSITE_BANDEAL.Admin
{
    public partial class BaiViet_Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Update bài viết";
            if (!IsPostBack)
            {
                LoadData();
            }


        }

        private void LoadData()
        {
            MA_LOAI_BV.DataSource = LOAI_BAI_VIETBUS.GetAll();
            MA_LOAI_BV.DataTextField = "TenloaI";
            MA_LOAI_BV.DataValueField = "Maloai";
            MA_LOAI_BV.DataBind();
            MA_LOAI_BV.Items.Insert(0, new ListItem("----NO PARENT---", "0"));
            //try
            //{
                int id = int.Parse(Request.QueryString["ID"]);
                BAI_VIET sp = BAI_VIETBUS.GetByID(id);
                if (sp != null)
                {
                    Ma.Value = sp.Mabv.ToString();
                    Ten.Value = sp.Tenbv.ToString();
                    MA_LOAI_BV.SelectedValue = sp.Maloaibv.ToString();          
                    NOI_DUNG.Text = sp.Noidung.ToString();
                }
            //}
            //catch
            //{
            //    Response.Redirect("BaiViet_List.aspx");
            //}
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            BAI_VIET bv = new BAI_VIET();
            bv.Mabv= int.Parse(Ma.Value);
            bv.Tenbv = Ten.Value;
            bv.Maloaibv = int.Parse(MA_LOAI_BV.SelectedValue);
            bv.Noidung = NOI_DUNG.Text;
            bv.Trangthai = 1;

            BAI_VIETBUS.update(bv);
            Response.Write("<script type='text/javascript'>alert('Update thành công'); window.opener.location.reload(true); window.close()</script>");
        }
    }
}